/*
 * Created by Huang 嵘才 on 18-6-24 上午9:58
 * Copyright (c) 2018. All rights reserved.
 * Last modified 18-6-24 上午9:58
 */

package io.esirong.net;

import io.reactivex.observers.DefaultObserver;

/**
 * 处理Http响应的基本回调 继承<extends>{@link DefaultObserver}
 * <P>处理http请求时异常Exception
 * <P>处理http请求响应时错误的业务数据（通过业务返回码ResCode,业务信息msg)
 *
 * @param <T> 继承于{@link BaseResponse} （它属于基本信息，通过获得信息做共用的处理）
 * @author HuangRc(黄嵘才)
 * @see RespHandler RespHandler
 */
public abstract class BaseObserver<T extends BaseResponse> extends DefaultObserver<T> {

    @Override
    public void onNext(T t) {
        boolean isSuccess = RespHandler.isOk(t);
        if (isSuccess) {
            onSuccess(t);
        } else {
            RespHandler.instance().handResp(t);
        }

    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        RespHandler.instance().handResponseError(e);
    }

    @Override
    public void onComplete() {

    }

    /**
     * 业务操作正常回调
     *
     * @param response 回调的对象 继承<extends>{@link BaseResponse}
     */
    protected abstract void onSuccess(T response);
}
